/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import client.MailClient;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Locale;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import server.MailServer;

/**
 *
 * @author leandro
 */
public class Index extends javax.swing.JFrame {

    /**
     * Creates new form Index
     */
     public MailClient cliente;

    public MailClient getCliente() {
        return cliente;
    }
    public Index() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("BIENVENIDOS");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("BIENVENIDOS");

        jButton1.setText("Conectarse");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jMenu1.setText("Opciones");

        jMenuItem1.setText("Crear Usuario");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Salir");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(51, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(46, 46, 46))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(78, 78, 78))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addGap(45, 45, 45)
                .addComponent(jButton1)
                .addContainerGap(45, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:       
     try{
          JFileChooser elegir = new JFileChooser();
          elegir.setCurrentDirectory(new java.io.File("."));
          int opcion = elegir.showOpenDialog(this);
              if (opcion == JFileChooser.APPROVE_OPTION) {
                String pathArchivo = elegir.getSelectedFile().getPath();    
                FileInputStream fis = new FileInputStream(pathArchivo);
                ObjectInputStream ois = new ObjectInputStream(fis);
                MailServer s = (MailServer) ois.readObject();
                ois.close();
                String nombre=JOptionPane.showInputDialog(this, "Ingresar Usuario");
                cliente=  new MailClient(s, nombre);
                if (!cliente.existe()){
                    
                 JOptionPane.showMessageDialog(this, "Usuario no existe");
                 return;
                }
                Bandeja bandeja= new Bandeja(cliente);
                bandeja.setTitle(cliente.nombreusuario);
                bandeja.setLocationRelativeTo(null);
                bandeja.setDefaultCloseOperation(bandeja.EXIT_ON_CLOSE);
                bandeja.setVisible(true);
               
                
              }
      }catch(FileNotFoundException e){
         e.printStackTrace();
      }catch(IOException e){
         e.printStackTrace();
      }catch(ClassNotFoundException e){
         e.printStackTrace();
      }
    }//GEN-LAST:event_jButton1ActionPerformed
     
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
            try{
          JFileChooser elegir = new JFileChooser();
           elegir.setCurrentDirectory(new java.io.File("."));
          int opcion = elegir.showOpenDialog(this);
              if (opcion == JFileChooser.APPROVE_OPTION) {
                String pathArchivo = elegir.getSelectedFile().getPath();    
                FileInputStream fis = new FileInputStream(pathArchivo);
                String archivo = elegir.getSelectedFile().getName();
                ObjectInputStream ois = new ObjectInputStream(fis);
                MailServer s = (MailServer) ois.readObject();
                ois.close();
                String nombre=JOptionPane.showInputDialog(this, "Ingresar Usuario");
                s.addUsuario(nombre);
                s.save();
             
                JOptionPane.showMessageDialog(this, "Usuario añadido exitosamente");
                
     
                
              }
      }catch(FileNotFoundException e){
         e.printStackTrace();
      }catch(IOException e){
         e.printStackTrace();
      }catch(ClassNotFoundException e){
         e.printStackTrace();
      }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        this.hide();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    /**
     * @param args the command line arguments
     */
  /* public static void main(String args[]) {
       
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Index index= new Index();
                index.setLocationRelativeTo(null);
                index.setDefaultCloseOperation(index.EXIT_ON_CLOSE);
                index.setVisible(true);
            }
        });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    // End of variables declaration//GEN-END:variables
}
